package com.renjun.smweb.controller.PermissionController;

import com.alibaba.fastjson.JSONObject;
import com.renjun.smweb.annotation.AuthorityDeclaration;
import com.renjun.smweb.commons.util.ResponseUtils;
import com.renjun.smweb.exceptions.DataNotFoundException;
import com.renjun.smweb.service.PermissionService.PermissionService;
import com.renjun.smweb.annotation.AuthorityDeclaration;
import com.renjun.smweb.commons.util.ResponseUtils;
import com.renjun.smweb.service.PermissionService.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;
import java.util.Map;

/**
 * Created by RenJun on 2017/2/21.
 * 权限管理M3
 */
@Controller
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    private PermissionService permissionService;

    /**
     * M3-123query角色查询（角色，菜单，数据，接口）
     *
     * @param {roleid:"2"}
     * @return [{"roleid":"2","rolename":"市管理员","roledesc":"市管理员","districtcode":"","roletype":"1","menus":[],"funcs":[],"buttons":[]}]
     * by huamin
     */
    @RequestMapping(value = "/getRoleConfigs", method = RequestMethod.POST)
    @ResponseBody
    @AuthorityDeclaration(id = "M3-123query", desc = "角色修改获取菜单、数据、接口",reqParam = "roleid",resBody = "roleid,rolename,roledesc,roletype,districtcode,menus,funcs,buttons",errMsgs = "sql异常")
    public JSONObject getRoleConfigs(HttpServletRequest request, HttpServletResponse response) {
        JSONObject rs = ResponseUtils.createErrorResponseBody(null);
        Map<String,Object> param = ResponseUtils.createRequestAndSessionLoginInfo(request);
        try {
            rs = permissionService.getRoleConfigs(param);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        }
        return rs;
    }

    /**
     * M3-123update角色修改（角色，菜单，数据，接口）
     *
     * @param {roleid:"2","menus":[],"funcs":[],"buttons"rolename:"2",roledesc:"2","districtcode":""}
     * @return
     * by huamin
     */
    @RequestMapping(value = "/configRole", method = RequestMethod.POST)
    @ResponseBody
    @AuthorityDeclaration(id = "M3-123update", desc = "保存角色菜单、数据、接口功能",reqParam = "roleid,menus,funcs,buttons,rolename,roledesc,districtcode",resBody = "修改成功",errMsgs = "sql异常")
    public JSONObject configRole(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = ResponseUtils.createRequestAndSessionLoginInfo(request);
        JSONObject rs = ResponseUtils.createErrorResponseBody(null);
        try {
            rs = permissionService.configRole(map);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        }
        return rs;
    }

    /**
     * M3-1所有菜单列表
     *
     * @param {}
     * @return
     * by huamin
     */
    @RequestMapping(value = "/queryAllMenus", method = RequestMethod.POST)
    @ResponseBody
    @AuthorityDeclaration(id = "M3-1", desc = "所有菜单列表",reqParam = "",resBody = "修改成功",errMsgs = "sql异常")
    public JSONObject queryAllMenus(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = ResponseUtils.createRequestAndSessionLoginInfo(request);
        JSONObject rs = ResponseUtils.createErrorResponseBody(null);
        try {
            rs = permissionService.queryAllMenus();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        }
        return rs;
    }

    /**
     * M3-3所有功能列表
     *
     * @param {}
     * @return
     * by huamin
     */
    @RequestMapping(value = "/queryAllFuncs", method = RequestMethod.POST)
    @ResponseBody
    @AuthorityDeclaration(id = "M3-3", desc = "所有功能列表",reqParam = "",resBody = "修改成功",errMsgs = "sql异常")
    public JSONObject queryAllFuncs(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = ResponseUtils.createRequestAndSessionLoginInfo(request);
        JSONObject rs = ResponseUtils.createErrorResponseBody(null);
        try {
            rs = permissionService.queryAllFuncs();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        }
        return rs;
    }

    /**
     * M3-4所有按钮列表
     *
     * @param {}
     * @return
     * by huamin
     */
    @RequestMapping(value = "/queryAllButtons", method = RequestMethod.POST)
    @ResponseBody
    @AuthorityDeclaration(id = "M3-4", desc = "所有按钮列表",reqParam = "",resBody = "修改成功",errMsgs = "sql异常")
    public JSONObject queryAllButtons(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = ResponseUtils.createRequestAndSessionLoginInfo(request);
        JSONObject rs = ResponseUtils.createErrorResponseBody(null);
        try {
            rs = permissionService.queryAllButtons();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        }
        return rs;
    }
    /**
     * M3-12根据角色ID获取菜单列表
     *
     * @param {roleid}
     * @return{id,sort,desc,roleid,name,lv,parentid}
     * by huamin
     */
    @RequestMapping(value = "/getMenuListByRoleId", method = RequestMethod.POST)
    @ResponseBody
    @AuthorityDeclaration(id = "M3-12", desc = "根据角色ID获取菜单列表",reqParam = "roleid",resBody = "id,sort,desc,roleid,name,lv,parentid",errMsgs = "sql异常")
    public JSONObject getMenuListByRoleId(HttpServletRequest request) {
        // TODO: 根据角色ID获取菜单列表
        Map<String, Object> map = ResponseUtils.createRequestAndSessionLoginInfo(request);
        JSONObject rs = ResponseUtils.createErrorResponseBody(null);
        try {
            rs = permissionService.getMenuListByRoleId(map);
        } catch (Exception e) {
            e.printStackTrace();
            rs.put("message", e.getMessage());
        }
        return rs;
    }

}
